/
[handle].reviews.ts
78 lines (66 loc) · 2.42 KB
/
[handle].reviews.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Data, DataItem, Route } from '@/types';
import { Context } from 'hono';
import { baseURL } from './const';
import got from '@/utils/got';
import { load } from 'cheerio';
export const route: Route = {
path: '/apps/:handle/reviews/:page?',
example: '/shopify/apps/flow/reviews',
parameters: { handle: '例如一个 App 的链接 https://apps.shopify.com/flow,其中 flow 就是指的是 handle' },
name: 'App reviews',
maintainers: ['PrintNow'],
handler,
radar: [
{
source: ['apps.shopify.com/:handle'],
},
],
};
async function handler(ctx: Context): Promise<Data> {
const { handle = '', page = '1' } = ctx.req.param();
const response = await got.get(`${baseURL}/${handle}/reviews`, {
searchParams: {
sort_by: 'newest',
page,
},
headers: {
accept: 'text/html, application/xhtml+xml',
'accept-language': 'en-US;q=0.9',
referer: baseURL,
dnt: '1',
},
});
const htmlContent = response.data;
const $ = load(htmlContent);
const items = $('div[data-merchant-review]')
.toArray()
.map((item) => {
const $item = $(item);
const reviewID = $item.attr('data-review-content-id');
const $review1 = $item.find('div:nth-child(1)');
const $review2 = $item.find('div:nth-child(2)');
const description = $item.find('div[data-truncate-review] div[data-truncate-content-copy] p').html() || '';
const author = $review2.find('div.tw-text-fg-primary').text().trim();
const result: DataItem = {
guid: reviewID,
title: description,
author,
pubDate: new Date($review1.find('div[aria-label] + div').text().trim()),
// 评论内容
description,
_extra: {
ratting_value: Number($review1.find('div[role="img"]').attr('aria-label')?.substring(0, 1)),
location: $review2.find('div.tw-text-fg-primary + div').text().trim(),
author,
},
};
return result;
});
return {
title: `Reviews handle:${handle} page:${page} – Shopify App Store`,
link: `${baseURL}/${handle}/reviews`,
allowEmpty: true,
language: 'en-US',
item: items,
};
}